package com.shengyi.service.impl;

import com.shengyi.common.BasicCode;
import com.shengyi.common.Result;
import com.shengyi.common.exception.ServiceException;
import com.shengyi.mapper.CarFinanceMapper;
import com.shengyi.model.CarFinance;
import com.shengyi.service.FinanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class FinanceServiceImpl implements FinanceService {
    @Autowired
    private CarFinanceMapper carFinanceMapper;

    /**
     * 在每天00：00：00时执行该方法
     */
    @Override
    @Scheduled(cron = " 0 0 0 * * ? ")
    public void addFinance() {
        CarFinance carFinance = new CarFinance();
        carFinance.setIncome(0.0);
        carFinance.setExpend(0.0);
        carFinanceMapper.insertFinance(carFinance);
    }

    @Override
    public Result<List<CarFinance>> getFinanceByTime(Date start, Date end) {
        if (start == null || end == null){
            throw new ServiceException(BasicCode.PARAM_NULL);
        }
        Result<List<CarFinance>> result = new Result<>();
        List<CarFinance> list = carFinanceMapper.selectFinanceByTime(start,end);
        return result.success(list);
    }
}
